잠시만 기다려 주세요

     '이태원 핼러윈 참사.. 150명 넘게 죽었는데 눈치나 보며 아무것도 안 하는 민주당을 규탄한다. 총선 보이콧... 문재인 시즌2 는 절대 금지다...'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (584)  |  끄적거림 (127)  |  문예 창작 (703)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - PostgreSQL의 트리거(TRIGGER)와 함수(function) 예제
이 름 : 바다아이   |   조회수 : 8997         짧은 주소 : https://www.bada-ie.com/su/?591591789061

PostgreSQL의 트리거와 펑션 예제를 만들어 봤다.

자료를 찾는데 너무 힘들어서 나중에 잊기전에 정리해봤다.

 

 

 

● 우선 테스트용 테이블 작성 

 

CREATE TABLE datalog(

    logtime timestamp PRIMARY KEY,

    data varchar,

    content varchar

);

 

* 테이블 지우기

DROP TABLE datalog;

 

● 펑션(Function) 함수 예제

    이 함수가 실행되면 지정 테이블의 레코드가 10개를 넘으면 가장 오래된 것부터 지운다.

 

CREATE FUNCTION data_fun() RETURNS trigger AS $data_fun$

    DECLARE

rowcount integer;

        delcount integer;

        maxcount integer;

    BEGIN

        maxcount := 10;

        SELECT count(*) logtime into rowcount  FROM datalog;

        delcount := rowcount - maxcount + 1;

 

        IF delcount > 0 THEN

   DELETE FROM datalog WHERE logtime IN (SELECT logtime FROM datalog ORDER BY logtime LIMIT delcount);

        END IF;

 

RETURN NEW;

    END;

$data_fun$ LANGUAGE plpgsql;

 

* 펑션 지우기

DROP FUNCTION data_fun();

 

● 트리거(Trigger) 함수 예제

    지정 Table에서 추가나 변경이 이루어질 때 지정 펑션이 실행된다.

 

CREATE TRIGGER data_trg BEFORE INSERT OR UPDATE ON datalog

    FOR EACH ROW EXECUTE PROCEDURE data_fun();

 

* 트리거 지우기

DROP TRIGGER data_trg ON datalog;

 

 

● 테스트용 데이터

 

insert into datalog(logtime, data) values('2015-01-01 12:12:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:09', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:09', 'aa');

 
위의 데이터를 입력하면 로그는 10개 행만 테이블에 저장된다.
데이터를 추가하기 전에 트리거의 연결된 펑션이 실행되어
테이블의 행이 10개를 넘어가면 가장 옛날 데이터를 삭제한다.


출처 : http://wwwi.tistory.com/348
 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 686
44 postgresql , count(*) .... 바다아이 6868
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 6102
42 postgresql sequence(퀀) 바다아이 7737
41 postgresql , , , index create, , 바다아이 8113
40 postgresql postgresql log_timezone .... 바다아이 6509
39 postgresql postgresql SEQUENCE reset .... 바다아이 8350
38 postgresql [PostgreSql] WITH , , Operator 바다아이 7743
37 postgresql postgresql for, foreach , 바다아이 9300
36 postgresql postgresql , , into ... 바다아이 9293
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 10712
34 postgresql postgresql ... .. , , 바다아이 11729
33 postgresql postgresql CSV export/import 바다아이 8532
32 postgresql postgresql tablespace , .... 바다아이 12860
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 9275
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 9233
현재글 postgresql PostgreSQL (TRIGGER) (function) 바다아이 8998
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 9647
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 9046
26 postgresql postgresql , .. 바다아이 9243
25 postgresql postgresql , size, 바다아이 11245
24 postgresql postgresql , , .... 바다아이 8283
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 10780
22 postgresql postgresql case 바다아이 8155
21 postgresql postgresql with 바다아이 8589
20 postgresql postgresql , , string 바다아이 11499
19 postgresql Postgresql partitioning table , , , 바다아이 9091
18 postgresql PostgreSQL 바다아이 10575
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 10264
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 9690
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.06249
to webmaster... gogo sea. gogo sea.